<template>
  <div class="line-box">
    <div class="time-line-head">
      <div class="head-txt">历史调度</div>
      <div class="info-btn" @click="goInfo">查看详情</div>
    </div>
    <div class="time-line-box">
      <a-timeline v-if="data.length > 0">
        <a-timeline-item color="#F5001D" v-for="item in data" :key="item.id">
          <div class="line-head">
            <div class="line-name">V-{{item.schemeInfo.number}}</div>
            <span class="line-time">{{item.schemeInfo.createDate}}</span>
          </div>
          <div class="line-body" @click="chartInfo(item)">
            <div class="line-item">
              <div class="line-item-li">
                <span>运费：</span>
                <span>{{item.schemeInfo.freight}}</span>
              </div>
              <div class="line-item-li">
                <span>贸易利润率：</span>
                <span>{{item.schemeInfo.trade}}</span>
              </div>
            </div>
            <div class="line-item">
              <div class="line-item-li">
                <span>时间：</span>
                <span>{{item.schemeInfo.days}}天</span>
              </div>
              <div class="line-item-li">
                <span>船舶利用率：</span>
                <span>{{item.schemeInfo.ship}}</span>
              </div>
            </div>
          </div>
        </a-timeline-item>
      </a-timeline>
      <EmptyDefault :style="{marginTop: '60px'}" v-else :descriptionInfo="'暂无数据'"/>
    </div>

  </div>

</template>

<script setup>
import { useRouter } from "vue-router";
import {defineEmits, defineProps, getCurrentInstance, onMounted, ref,watch} from "vue";
const { proxy  } = getCurrentInstance()
import EmptyDefault from '@/components/EmptyDefault/EmptyDefault.vue'
const emit = defineEmits(['historyChange'])
const router = useRouter()
const props = defineProps({
  isAdd: {
    type: Boolean,
    default: false
  },
  planId: {
    type: String,
    default: ''
  }
})
const planId = ref(props.planId)
watch(() => props.planId, (newVal) => {
  planId.value = newVal
  getList()
})
watch(() => props.isAdd, () => {
  getList()
})
const goInfo = ()=> {
  router.push({
    path: '/schedulingHistory',
    query: {
      planId: planId.value
    }
  })
}
const data = ref([])
const getList = () => {
  proxy.$api.postScheduilingHistory({ year: null, planId: planId.value}).then(res => {
    data.value = []
    let dataArr =[]
    if (res.resultObject) {
      dataArr.push(res.resultObject[0])
    }
    dataArr.map(item => {
      if (item.shipNames && item.shipNames.length > 0) {
        item.shipNames.map(item => {
          Object.assign(item, {
            details: resetData(item.details)
          })
        })
      }
      if (item.goodsNames && item.goodsNames.length > 0) {
        item.goodsNames.map(item => {
          Object.assign(item, {
            details: resetData(item.details)
          })
        })
      }
      if (item.sellerNames && item.sellerNames.length > 0) {
        item.sellerNames.map(item => {
          Object.assign(item, {
            details: resetData(item.details)
          })
        })
      }
      data.value.push(item)
    })
    console.log('历史调度', data.value)
  })
}
const resetData = (arr) => {
  let newArr  = []
  arr.map(item => {
    if (item.expectedLoadingDate && item.expectedUnloadingWindow) {
      newArr.push(item)
    }
  })
  return newArr
}
/**
 * 点击历史方案，左侧甘特图数据跟着变
 * */
const chartInfo = (val) => {
  emit('historyChange', val)
}
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.line-box {
  background: #ffffff;
  border-radius: 2px;
  padding: 2px;
}
.time-line-head {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
  padding: 18px 8px 18px 24px;
  .head-txt {
    line-height: 32px;
    color: #2C3749;
    font-size: 18px;
  }
  .info-btn {
    width: 96px;
    height: 32px;
    line-height: 32px;
    background: rgba(0,98,173,0.08);
    border: 1px solid rgba(0,98,173,0.08);
    border-radius: 3px;
    color: #0062AD;
    cursor: pointer;
  }
}
.time-line-box {
  padding: 0 8px 0 24px;
}
.line-head {
  text-align: left;
  display: flex;
  justify-content: space-between;
  .line-name {
    color: #2C3749;
  }
  .line-type {
    color: #0062AD;
    font-size: 16px;
  }
  .line-type-1 {
    color: #F5001D;
  }
  .line-time {
    color: #999999;
    font-size: 12px;
  }
}
.line-body {
  margin-top: 10px;
  padding: 20px 20px 10px 20px;
  background: #F7F8FA;
  cursor: pointer;
  .line-item {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
    .line-item-li {
      font-size: 14px;
      color: #2C3749;
    }
  }
}
</style>
